package com.atbun.orderservice.service.impl;

import com.atbun.commonutils.R;
import com.atbun.commonutils.vo.EduCourseOrderVo;
import com.atbun.commonutils.vo.UcenterMemberVo;
import com.atbun.orderservice.client.EduClient;
import com.atbun.orderservice.client.UcenterClient;
import com.atbun.orderservice.entity.Order;
import com.atbun.orderservice.mapper.OrderMapper;
import com.atbun.orderservice.service.OrderService;
import com.atbun.orderservice.utils.OrderNoUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-05-20
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
//    远程调用edu与ucenter
    @Autowired
    private EduClient eduClient;

    @Autowired
    private UcenterClient ucenterClient;


    /**
     * 创建订单
     * @param courseId
     * @param memberId
     * @return
     */
    @Override
    public String createOrder(String courseId, String memberId) {
//        获取课程信息
        EduCourseOrderVo courseOrder = eduClient.getCourseOrderById(courseId);
//        获取用户信息
        UcenterMemberVo ucenterInfo = ucenterClient.getUcenterInfo(memberId);

        //创建订单
        Order order = new Order();
        order.setOrderNo(OrderNoUtil.getOrderNo());
        order.setCourseId(courseId);
        order.setCourseTitle(courseOrder.getTitle());
        order.setCourseCover(courseOrder.getCover());
        order.setTeacherName(courseOrder.getTeacherName());
        order.setTotalFee(courseOrder.getPrice());
        order.setMemberId(memberId);
        order.setMobile(ucenterInfo.getMobile());
        order.setNickname(ucenterInfo.getNickname());
        order.setStatus(0);
        order.setPayType(1);
        baseMapper.insert(order);

        return order.getOrderNo();
    }
}
